package ru.dc.gui.swing.settings;

import com.jidesoft.dialog.AbstractDialogPage;
import ru.dc.Application;
import ru.dc.object.Settings;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 02.11.2010
 * Time: 16:58:14
 * </pre>
 */
public class ConnectionPage  extends AbstractDialogPage {
    private JRadioButton passive;
    private JRadioButton active;
    private JTextField extAddr;
    private JCheckBox getAddrFromHub;
    private JSpinner udpPort;
    private JSpinner tcpPort;
    private JPanel rootPanel;

    public ConnectionPage() {
        setTitle("Соединение");


        setLayout(new BorderLayout());
        add(rootPanel, BorderLayout.CENTER);

    }

    public void lazyInitialize() {
        final Settings settings = Application.getApplication().getSettings();
        if (settings.getMode()==Settings.MODE_ACTIVE) {
            active.setSelected(true);
        }
        else {
            passive.setSelected(true);
        }

        extAddr.setText(settings.getExternalAddress()!=null ? settings.getExternalAddress() : "");
        getAddrFromHub.setSelected(settings.isGetAddressFromHub());
        udpPort.setValue(settings.getUdpPort());
        tcpPort.setValue(settings.getTcpPort());


    }

    public void apply() {
        final Settings settings = Application.getApplication().getSettings();
        settings.setMode(active.isSelected()? Settings.MODE_ACTIVE : Settings.MODE_PASSIVE);
        settings.setExternalAddress(extAddr.getText());
        settings.setGetAddressFromHub(getAddrFromHub.isSelected());
        settings.setUdpPort(new Integer(udpPort.getValue().toString()));
        settings.setTcpPort(new Integer(tcpPort.getValue().toString()));
    }

}
